package org.kaer.election.utils;

import org.json.JSONObject;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.util.Date;

public class CommonUtil
{
    public static boolean IsCardNumberValid( String cardNo )
    {
    	if( cardNo == null || cardNo.length() != 16 )
    		return false;
    	for( int i = 0; i < cardNo.length(); ++i )
    	{
    		char ch = cardNo.charAt(i);
    		if( !Character.isDigit(ch) )
    			return false;
    	}
    	return true;
    }
    
    public static boolean IsCardPasswordValid( String cardPwd )
    {
    	return IsCardNumberValid(cardPwd);
    }
    
	public static String JPointToMoneyString( int jpoint )
	{
		/*
		String str = Integer.toString(jpoint);
		if( str.length() <= 2)
		{
			str = "000" + str;
			str = str.substring(str.length()-3, str.length());
		}
		return str.substring(0, str.length()-2) + "." + str.substring(str.length()-2, str.length());*/
    	DecimalFormat df = new DecimalFormat("##0.00");
    	String strAmt = df.format(jpoint/100.0f);
    	strAmt.replace(".00", "");
    	//Log.v("ChargeHeepay", "getRealJPointString payJPoint=" + jpoint + ", strAmt=" + strAmt);
    	return strAmt;
	}

	public static int StrToInt(String str)
	{
		return StrToInt(str,0);
	}
	
	public static int StrToInt(String str, int defValue)
	{
		try {
			return Integer.parseInt(str);
		}catch(Exception ex)
		{
			return defValue;
		}
	}
	
	public static int GetJsonIntValue(JSONObject obj, String key, int defValue)
	{
		try
		{
			if( obj.has(key) )
				return obj.getInt(key);
			
			return defValue;
		}
		catch( Exception ex )
		{
			return defValue;
		}
	}
	public static long GetJsonLongValue(JSONObject obj, String key, long defValue)
	{
		try
		{
			if( obj.has(key) )
				return obj.getLong(key);
			
			return defValue;
		}
		catch( Exception ex )
		{
			return defValue;
		}
	}
	public static double GetJsonDoubleValue(JSONObject obj, String key, double defValue)
	{
		try
		{
			if( obj.has(key) )
				return obj.getDouble(key);
			
			return defValue;
		}
		catch( Exception ex )
		{
			return defValue;
		}
	}
	public static String GetJsonStringValue(JSONObject obj, String key)
	{
		try
		{
			if( obj.has(key) )
				return obj.getString(key);
			
			return null;
		}
		catch( Exception ex )
		{
			return null;
		}
	}
	
	public static String GetBillStatusString(int billStatus)
	{
		switch( billStatus )
		{
		case -1: return "ʧ��";
		case 1: return "�ɹ�";
		case 0: return "������";
		default: return "δ֪";
		}
	}
	
	public static String GetNowTimeString()
	{
		Date now = new Date();
		String createTime = DateFormat.getInstance().format(now);
		return createTime;
	}
}
